/**
 * @author helio
 */
var ProductsManageView = Ext.extend(BasicPanel,{
	inputWin : null,
	
	initlize : function() {
		
		if(!this.detailsGrid) {
						
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'products',
					remoteSort: true,
			        fields: ['id','code','name', 'abbr', 'model', 'specs', 'unit', 'manufacturer', 'origin']
			    }),
				sortInfo:{field: 'code', direction: 'ASC'}
			});
			
			// create paging bar
			var pagingBar = new Ext.PagingToolbar({
		        pageSize: 25,
		        store: this.detailsStore,
		        displayInfo: true,
				
				plugins: new Ext.ux.ProgressBarPager()
		    });
			
			// create the grid
			var csm = new Ext.grid.CheckboxSelectionModel();
			
			this.detailsGrid = new Ext.grid.GridPanel({
				store: this.detailsStore,
				columns: [csm,{
					header: "商品编号",
					dataIndex: 'code',
					width: 60,
					sortable: true
				}, {
					header: "商品名称",
					dataIndex: 'name',
					sortable: true
				}, {
					header: "商品简称",
					dataIndex: 'abbr',
					width: 80,
					sortable: true
				}, {
					header: "商品型号",
					dataIndex: 'model',			
					width: 80,
					sortable: true
				}, {
					header: "商品规格",
					dataIndex: 'specs',
					width: 80,
					sortable: true
				}, {
					header: "单位",
					dataIndex: 'unit',
					width: 45,
					sortable: true
				}, {
					header: "生产商",
					dataIndex: 'manufacturer',
					sortable: true
				}],
				
				sm: csm,
				
				tbar:[{
            		xtype:'splitbutton',
            		text: '添加商品',
            		iconCls: 'add',
            		handler: this.newProduct.createDelegate(this),
           			menu: [{
						text: '单个添加', 
						handler: this.newProduct.createDelegate(this)
					}, {
						text: '批量添加', 
						handler: this.showBatchInsertProductsWin.createDelegate(this)
					}]
        		}, '-', {
		            text:'修改商品',
		            tooltip:'修改商品信息',
		            iconCls:'option',
					handler: this.modifyProduct.createDelegate(this)
		        }, '-', {
		            text:'删除',
		            tooltip:'删除选中的商品',
		            iconCls:'remove',
					handler: this.deleteSelectedProducts.createDelegate(this)
		        }, '->', {
            		xtype:'splitbutton',
            		text: '销售状况',
            		iconCls: 'chart-line-link',
            		handler: this.showProductRecentlyDateStatusLineChart.createDelegate(this),
           			menu: [{
						text: '销售趋势',
						iconCls: 'chart-line',
						handler: this.showProductRecentlyDateStatusLineChart.createDelegate(this)
					},{
						text: '销售统计',
						iconCls: 'chart-bar',
						handler: this.showProductRecentlyDateStatusColumnChart.createDelegate(this)
					}]
        		}, '-', new Ext.form.ComboBox({
					id: 'productsquerytype',
					tpl: comboxTpl,
				    triggerAction: 'all',
				    lazyRender:true,
				    forceSelection:true,
                 	selectOnFocus: true,
				    mode: 'local',
				    width: 100,
				    store: new Ext.data.ArrayStore({
				        id: 0,
				        fields: [
				            'code',
				            'name'
				        ],
				        data: [[1, '按编号查询'], [2, '按名称查询'], [3, '按规格查询'], [4, '按型号查询']]
				    }),
				    valueField: 'code',
				    displayField: 'name',
				    value:2
				}), '-', {
					id: 'productsqueryvalue',
                	xtype:'trigger',
                    triggerClass: 'x-form-search-trigger',
                    width: 100,
                    onTriggerClick: this.queryProducts.createDelegate(this),
                    listeners: {
        				'show': this.queryProducts.createDelegate(this)
        			}
                }],
				
				bbar: pagingBar,
				
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
		
				loadMask: true,
				stripeRows: true,
				border: false,
				autoExpandColumn: 'code',
				layout: 'fit',
				
				listeners: {
					cellclick : function(grid, rowIndex, columnIndex, e) {
						this.selectedRowIndex = rowIndex;
					}.createDelegate(this),
					
					rowdblclick : this.modifyProduct.createDelegate(this)
				}
			});
		}
		
		if(!SALE.view.chartView) {
			SALE.view.chartView = new SaleChartView();
		}
	},
	
	initlizeDataHandler : function(resultJson) {
		this.detailsJson['products'] = resultJson.products;
		this.detailsStore.load({params:{start:0, limit:25}});
	},
	
	getCorePanel : function() {
		if (!this.detailsGrid) {
			this.initlize();
			SALE.Action.getProductsListJson(this.initlizeDataHandler.createDelegate(this));
		}
		return this.detailsGrid;
	},
	
	queryProducts : function() {
		var querytype = Ext.getCmp('productsquerytype').getValue();
		var queryvalue = Ext.getCmp('productsqueryvalue').getValue();
		var condition = {};
		if(querytype == 1) {
			condition.code = queryvalue;
		} else if(querytype == 2) {
			condition.name = queryvalue;
		} else if(querytype == 3) {
			condition.specs = queryvalue;
		} else if(querytype == 4) {
			condition.model = queryvalue;
		} 
		
		if(queryvalue){
			SALE.Action.getProductsListJson(this.initlizeDataHandler.createDelegate(this), condition);
		}
	},
	
	showEditeWin : function() {
		SALE.window.showProductsEditWindow(this.refreshGridRecord.createDelegate(this),this.formDataJson, this.task);
	},
	
	showBatchInsertProductsWin : function() {
		if(!this.batchWin) {		
			this.batchWin = new Ext.Window({
			 		
			 	title: '批量添加商品信息',
				closable: true,	
				collapsible  : true,
				maximizable: true,
				width: 800,
				height: 560,
				layout: 'fit',
				closeAction :'hide',
						
				items: {
					border: false,
					xtype : "productsbatchinsert"
				},
				
				listeners: {
					'hide': function() {
						SALE.Action.getProductsListJson(this.initlizeDataHandler.createDelegate(this));
					}.createDelegate(this)
				}
			});
		}
		this.batchWin.show();
	},
	
	newProduct : function() {
		this.task = "new";
		this.formDataJson = {};
		this.showEditeWin();
	},
	
	modifyProduct : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			SALE.Utils.showMessageBox('请先选择一条数据！');
		} else if(recordList.length > 1) {
			SALE.Utils.showMessageBox('每次只能编辑一条数据，请确认只选中了一条数据！');
		} else {
			var id= recordList[0].get("id");
			
			SALE.Action.searchProductJson(this.showModifyDataHandler.createDelegate(this), id);
		}
	},
	
	deleteSelectedProducts : function() {
		this.selectedRecordList = this.detailsGrid.getSelectionModel().getSelections();
		if(this.selectedRecordList.length == 0) {
			SALE.Utils.showMessageBox('请选择你要删除的数据！');
		} else {
			Ext.MessageBox.confirm('删除确认', '你真的要删除选中的商品信息吗?', function(btn) {
				if (btn == "yes") {
					SALE.Action.deleteProductsInfo(this.deleteSuccessHandler.createDelegate(this), this.selectedRecordList);
				}
			}.createDelegate(this));
		}
	},
	
	showProductRecentlyDateStatusLineChart : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			this.warnMessage('请先选择一条数据！！！', '警告信息');
		} else if(recordList.length > 1) {
			this.warnMessage('每次只能编辑一条数据，请确认只选中了一条数据！！！', '警告信息');
		} else {
			var code = recordList[0].get("code");			
			var title = "近日销售状况";
			SALE.Action.searchProductRecentlyDateStatus(this.showLineChartHandler.createDelegate(this), {'title': title}, code);
		}		
	},
	
	showProductRecentlyDateStatusColumnChart : function() {
		var recordList = this.detailsGrid.getSelectionModel().getSelections();
		if(recordList.length == 0) {
			this.warnMessage('请先选择一条数据！！！', '警告信息');
		} else if(recordList.length > 1) {
			this.warnMessage('每次只能编辑一条数据，请确认只选中了一条数据！！！', '警告信息');
		} else {
			var code = recordList[0].get("code");			
			var title = "近日销售状况";
			SALE.Action.searchProductRecentlyDateStatus(this.showColumnChartHandler.createDelegate(this), {'title': title}, code);			
		}				
	}
});

function showProductsPanel(){
	var  productsView = new ProductsManageView();
	
	SALE.App.showOperateTabPanel(productsView.getCorePanel(), "商品信息编辑");
}